package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TerminalWroExamDetailActivity;
import com.hctforgreen.greenservice.examine.WroExamDetailActivity;
import com.hctforgreen.greenservice.model.ExamSubEntity;
import com.hctforgreen.greenservice.model.AllExamListEntity.ExamEntity;
import com.hctforgreen.greenservice.service.HctApplication;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.ScreenUtil;

/**
 * 
 * 终端培训：
 * 答题列表GV适配器
 * 2016-9-22
 * @author 390694
 * 
 */
public class ShowTerminalSituationListAdapter extends BaseAdapter {

	private Activity mActivity;
	private LayoutInflater mInflater;
	private List<ExamSubEntity> mEntities;
	/**
	 * 
	 * 终端培训：
	 * 答题列表GV适配器：答案解析
	 * 
	 * 2016-9-22
	 * @author 390694
	 * 
	 */
	public ShowTerminalSituationListAdapter(Activity activity, GridView gridView,
			List<ExamSubEntity> entities) {
		this.mActivity = activity;
		mInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mEntities = entities;
		gridView.setAdapter(this);
	}

	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		convertView = mInflater.inflate(R.layout.item_not_yet_lst, null);
		final ExamSubEntity entity = (ExamSubEntity) getItem(position);
		final TextView indexTv = (TextView) convertView
				.findViewById(R.id.tv_index);
		indexTv.setText(String.valueOf(position + 1));

		FrameLayout selectorLyt = (FrameLayout) convertView
				.findViewById(R.id.lyt_selector);

		setSelectorLayoutParams(selectorLyt);

		/**
		 * 全对
		 */
		if (entity.inputAnswerResult
				.equals(HctConstants.INPUT_ANSWER_RESULT_ALL_RIGHT)) {
			indexTv.setBackgroundResource(R.drawable.right_color);
		}
		/**
		 * 半对、全错
		 */
		else if (entity.inputAnswerResult
				.equals(HctConstants.INPUT_ANSWER_RESULT_HALF_RIGHT)
				|| entity.inputAnswerResult
						.equals(HctConstants.INPUT_ANSWER_RESULT_ALL_WRONG)) {
			indexTv.setBackgroundResource(R.drawable.wrong_color);

		}
		/**
		 * 未做
		 */
		else if (entity.inputAnswerResult
				.equals(HctConstants.INPUT_ANSWER_RESULT_EMPTY)) {
			indexTv.setBackgroundResource(R.drawable.not_yet_color);
			// selectorLyt.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// Intent intent = new Intent();
			// intent.setClass(mActivity, WroExamDetailActivity.class);
			// intent.putExtra(HctConstants.ON_ACTIVITY_KEY_EXAM_ENTITY,
			// entity);
			// mActivity.startActivity(intent);
			// }
			// });
		}
		/**
		 * 答案解析
		 */
		selectorLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(mActivity, TerminalWroExamDetailActivity.class);
				intent.putExtra("position", position);
				HctApplication.terminalExamSubEntities = mEntities;
				mActivity.startActivity(intent);
			}
		});
		return convertView;
	}

	private void setSelectorLayoutParams(FrameLayout selectorLyt) {
		ScreenUtil screenUtil = new ScreenUtil();
		int screenWidth = screenUtil.getScreenWidth(mActivity);
		android.widget.LinearLayout.LayoutParams params = new LayoutParams(
				screenWidth / 5, screenWidth / 5);
		selectorLyt.setLayoutParams(params);
	}

}
